/*     */ package com.zimbra.cs.server;
/*     */ 
/*     */ import com.google.common.base.CharMatcher;
/*     */ import com.zimbra.common.util.Log;
/*     */ import java.io.IOException;
/*     */ import java.nio.ByteBuffer;
/*     */ import org.apache.mina.core.buffer.IoBuffer;
/*     */ import org.apache.mina.core.filterchain.IoFilter.NextFilter;
/*     */ import org.apache.mina.core.filterchain.IoFilterAdapter;
/*     */ import org.apache.mina.core.session.IoSession;
/*     */ import org.apache.mina.core.write.WriteRequest;
/*     */ import org.apache.mina.filter.codec.ProtocolDecoderException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class NioLoggingFilter
/*     */   extends IoFilterAdapter
/*     */ {
/*     */   private final Log log;
/*     */   private final boolean hexDump;
/*     */   
/*     */   NioLoggingFilter(NioServer server, boolean hexDump)
/*     */   {
/*  40 */     this.log = server.getLog();
/*  41 */     this.hexDump = hexDump;
/*     */   }
/*     */   
/*     */   public void exceptionCaught(IoFilter.NextFilter next, IoSession session, Throwable cause)
/*     */   {
/*  46 */     NioHandlerDispatcher.getHandler(session).setLoggingContext();
/*  47 */     if (((cause instanceof IOException)) || ((cause instanceof ProtocolDecoderException)))
/*     */     {
/*  49 */       this.log.debug(cause, cause);
/*     */     } else {
/*  51 */       this.log.error(cause, cause);
/*     */     }
/*  53 */     next.exceptionCaught(session, cause);
/*     */   }
/*     */   
/*     */   public void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message)
/*     */   {
/*  58 */     NioHandlerDispatcher.getHandler(session).setLoggingContext();
/*  59 */     if (this.log.isTraceEnabled()) {
/*  60 */       this.log.trace("C: %s", new Object[] { pp(message) });
/*     */     }
/*  62 */     nextFilter.messageReceived(session, message);
/*     */   }
/*     */   
/*     */   public void filterWrite(IoFilter.NextFilter next, IoSession session, WriteRequest req)
/*     */   {
/*  67 */     if (this.log.isTraceEnabled()) {
/*  68 */       this.log.trace("S: %s", new Object[] { pp(req) });
/*     */     }
/*  70 */     next.filterWrite(session, req);
/*     */   }
/*     */   
/*     */   private Object pp(Object msg) {
/*  74 */     if ((msg instanceof WriteRequest)) {
/*  75 */       return pp(((WriteRequest)msg).getMessage());
/*     */     }
/*  77 */     if ((msg instanceof String)) {
/*  78 */       return msg;
/*     */     }
/*     */     ByteBuffer bb;
/*  81 */     if ((msg instanceof ByteBuffer)) {
/*  82 */       bb = (ByteBuffer)msg; } else { ByteBuffer bb;
/*  83 */       if ((msg instanceof IoBuffer)) {
/*  84 */         bb = ((IoBuffer)msg).buf(); } else { ByteBuffer bb;
/*  85 */         if ((msg instanceof byte[])) {
/*  86 */           bb = ByteBuffer.wrap((byte[])msg);
/*     */         } else
/*  88 */           return msg; } }
/*     */     ByteBuffer bb;
/*  90 */     return isPrintable(bb) ? pp(bb) : msg;
/*     */   }
/*     */   
/*     */   private boolean isPrintable(ByteBuffer bb) {
/*  94 */     int limit = bb.limit();
/*  95 */     for (int i = bb.position(); i < limit; i++) {
/*  96 */       if (!isPrintable(bb.get(i))) return false;
/*     */     }
/*  98 */     return true;
/*     */   }
/*     */   
/*     */   private static boolean isPrintable(byte b) {
/* 102 */     switch (b) {
/*     */     case 9: case 10: case 13: 
/* 104 */       return true;
/*     */     }
/* 106 */     return (b > 31) && (b < Byte.MAX_VALUE);
/*     */   }
/*     */   
/*     */   private String pp(ByteBuffer bb)
/*     */   {
/* 111 */     StringBuilder sb = new StringBuilder(bb.remaining());
/* 112 */     if (this.hexDump) {
/* 113 */       sb = NioUtil.appendHex(sb.append('('), bb).append(") ");
/*     */     }
/* 115 */     int limit = bb.limit();
/* 116 */     for (int i = bb.position(); i < limit; i++) {
/* 117 */       sb.append((char)(bb.get(i) & 0xFF));
/*     */     }
/* 119 */     return CharMatcher.anyOf("\r\n").trimTrailingFrom(sb);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/server/NioLoggingFilter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */